var fs = require('fs'),
    formidable = require('formidable'),
    http = require('http');
    console.log(333333);
exports.upload = function(req, res) {
    // console.log(req);
    var cookies = req.cookies;
    var form;
    var uploadUrl = req.url;
    console.log(999);
    console.log(uploadUrl);
    form = new formidable.IncomingForm();
    form.parse(req, function(err, fields, files) {
        console.log(fields);
        console.log(files);
        console.log(fields.houseCode);
        console.log(888);
        var $files, boundaryKey, content, contentBinary, contentLength, enddata, error, fileStream, i, options, p, postheaders, req, stat, success, _i, _len;
        boundaryKey = Math.random().toString(16);
        enddata = '\r\n----' + boundaryKey + '--';
        var pathlist = [];
        $files = new Array();
        // content += "\r\n----" + boundaryKey + "\r\n" + "Content-Disposition: form-data; name=\"" + "fileFolder" + "\"\r\n" + "\r\n" + fields.fileFolder;
        // content += "\r\n----" + boundaryKey + "\r\n" + "Content-Disposition: form-data; name=\"" + "agentId" + "\"\r\n" + "\r\n" + fields.agentId;
        content += "\r\n----" + boundaryKey + "\r\n" + "Content-Disposition: form-data; name=\"" + "houseCode" + "\"\r\n" + "\r\n" + fields.houseCode;


        for (var p in files) {
            var object = files[p];
            content += "\r\n----" + boundaryKey + "\r\n" + "Content-Type: " + object.type + "\r\n" + "Content-Disposition: form-data; name=\"" + p + "\"; filename=\"" + object.name + "\"\r\n" + "Content-Transfer-Encoding: binary\r\n\r\n";

            pathlist.push(object.path);
        }

        // content += "\r\n----" + boundaryKey;
        console.log(content);
        contentBinary = new Buffer(content, 'utf-8');
        console.log(contentBinary);
        $files.push({
            contentBinary: contentBinary,
            filePath: pathlist
        });
        contentLength = 0;
        for (p = _i = 0, _len = $files.length; _i < _len; p = ++_i) {
            i = $files[p];

            contentLength += i.contentBinary.length;
            for (var l = 0; l < i.filePath.length; l++) {
                stat = fs.statSync(i.filePath[l]);
                contentLength += stat.size;
            }

        }

        postheaders = options = {
            'host': 'qd.innjia.com',
            'port': '8039',
            'path': uploadUrl,
            'method': 'POST',
            'headers': postheaders
        };

        req = http.request(options, function(res) {
            res.on("data", function(chunk) {
                success(chunk);
            });
        });
        req.setHeader("Content-Type", 'multipart/form-data; boundary=--' + boundaryKey);
        req.setHeader("Content-Length", contentLength + Buffer.byteLength(enddata));
        req.on('error', function(e) {
            return error(e);
        });

        req.write($files[0].contentBinary);
        fileStream = fs.createReadStream(pathlist[0], {
            bufferSize: 4 * 1024
        });
        fileStream.pipe(req, {
            end: false
        });
        fileStream.on('end', function() {
            return req.end(enddata);
        });
        success = function(d) {
            var $json, $str;
            $str = d.toString();
            console.log(d);
            console.log($str);
            $json = JSON.parse($str);
            return res.status(200).send($json).end();
        };
        error = function(d) {
            var $json, $str;
            $str = d;
            $json = JSON.parse($str);
            return res.status(500).send($json).end();
        };
    });
}